# -*- coding: utf-8 -*-

from __future__ import with_statement
import ConfigParser
import os


def read_conf():
    config = ConfigParser.RawConfigParser()
    lista_parsed = config.read(os.path.expanduser('~/.imnext'))

    if len(lista_parsed) == 0:
        return {}

    sections_list = config.sections()
    user_data = {}
    if 'XMPP' or 'FRIENDS' or 'POSITION' in sections_list:
        user_data['username'], user_data['password'] = read_user( config )
        user_data['friends_list'] = read_friends_list( config )
        user_data['user_position'] = read_user_position( config )
        user_data['user_location'] = read_user_location( config )

    return user_data


def read_user_position( config ):
    position = config.get('POSITION', 'position')

    position = eval(position)

    return position


def read_user_location( config ):
    position = config.get('POSITION', 'location')

    return position

    
def read_user( config ):
    username = config.get('XMPP', 'username')
    password = config.get('XMPP', 'password')
    
    return username, password


def read_friends_list( config ):
    friends_list = config.get('FRIENDS', 'friend')
    if friends_list <> "":
        friends_list = eval(friends_list)

    return friends_list


def write_conf(user_data_conf):
    config = ConfigParser.RawConfigParser()

    write_user( user_data_conf, config )
    write_friends_list( user_data_conf, config )
    write_user_position( user_data_conf, config )

    with open( os.path.expanduser('~/.imnext'), 'wb' ) as configfile:
        config.write(configfile)


def write_user( user_data_conf, config ):
    config.add_section('XMPP')
    if user_data_conf['username'] and user_data_conf['password']:        
        config.set('XMPP', 'username', user_data_conf['username'])
        config.set('XMPP', 'password', user_data_conf['password'])
    else:
        config.set('XMPP', 'username', '')
        config.set('XMPP', 'password', '')
        

def write_friends_list( user_data_conf, config ):
    config.add_section('FRIENDS')
    if user_data_conf['friends_list']:
        config.set('FRIENDS', 'friend', user_data_conf['friends_list'])
    else:
        config.set('FRIENDS', 'friend', '')


def write_user_position( user_data_conf, config ):
    config.add_section('POSITION')
    if user_data_conf['user_position']:
        config.set('POSITION', 'position', user_data_conf['user_position'])
    else:
        config.set('POSITION', 'position', (0,0) )
        
    if user_data_conf['user_location']:
        config.set('POSITION', 'location', user_data_conf['user_location'])
    else:
        config.set('POSITION', 'location', '')
        


def fill_conf_data( cli, conf_data ):
    fill_conf_user( cli, conf_data )
    fill_conf_friends_list( cli, conf_data )
    fill_conf_user_position( cli, conf_data )

    
def fill_conf_user( cli, conf_data ):
    conf_data['username'] = cli.get_username()
    conf_data['password'] = cli.get_password()


def fill_conf_friends_list( cli, conf_data ):
    conf_data['friends_list'] = cli.get_friends_list()


def fill_conf_user_position( cli, conf_data ):
    conf_data['user_position'] = cli.get_position()
    conf_data['user_location'] = cli.get_location()
    
